package com.example.ten_minutes_more;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	ListView list;
	public static Setting_adapter adapter;
	public static ArrayList<Alarm_data> data;
	static Alarm_data da;
	TextView add_t;
	ImageButton add;
	Typeface font;
	public static Database db;
	static Cursor result = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_alarm);
        
        
        
        add_t = (TextView)findViewById(R.id.add_text);
        font = Typeface.createFromAsset(getAssets(), "font1.TTF");
        add_t.setTypeface(font, Typeface.BOLD);
        add_t.setTextSize(20);
        add_t.setOnClickListener(addListener_t);
        
        add = (ImageButton)findViewById(R.id.add);
        add.setOnClickListener(addListener);
        
        db = new Database(this);
        db.open();
        
        setData();
        
        adapter = new Setting_adapter(this, data);
        list = (ListView)findViewById(R.id.listView1);
	    list.setAdapter(adapter);
	    
    }
    
  
	TextView.OnClickListener addListener_t = new TextView.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Setting_alarm.ISNEW = true;
			Intent info = new Intent(MainActivity.this, Setting_alarm.class);
			startActivity(info);
		}

    };
    
    ImageButton.OnClickListener addListener = new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			Setting_alarm.ISNEW = true;
			Intent info = new Intent(MainActivity.this, Setting_alarm.class);
			startActivity(info);
		}
    	
    };
    
    
    public static void setData(){
    	int hour = 0, minute = 0;
    	String am_pm = "";
    	String time = "";
    	int mon = 0, tue = 0, wed = 0, thu = 0, fri = 0, sat = 0, sun = 0;
    	int mon_image = R.drawable.monday_list, 
    		tue_image = R.drawable.tuesday_list, 
    		wed_image = R.drawable.wednesday_list, 
    		thu_image = R.drawable.thursday_list, 
    		fri_image = R.drawable.friday_list, 
    		sat_image = R.drawable.saturday_list, 
    		sun_image = R.drawable.sunday_list;
    	
    	result = db.getAlarmList();
    	
    	data = new ArrayList<Alarm_data>();
 
    	
    	if(result.moveToFirst()){
			do{
				hour = result.getInt(0);
				minute = result.getInt(1);
				time = hour + "시 " + minute + "분";
				am_pm = result.getString(2);
				if((mon = result.getInt(3)) == 1)
					mon_image = R.drawable.monday_list_checked;
				if((tue = result.getInt(4)) == 1)
					tue_image = R.drawable.tuesday_list_checked;
				if((wed = result.getInt(5)) == 1)
					wed_image = R.drawable.wednesday_list_checked;
				if((thu = result.getInt(6)) == 1)
					thu_image = R.drawable.thursday_list_checked;
				if((fri = result.getInt(7)) == 1)
					fri_image = R.drawable.friday_list_checked;
				if((sat = result.getInt(8)) == 1)
					sat_image = R.drawable.saturday_list_checked;
				if((sun = result.getInt(9)) == 1)
					sun_image = R.drawable.sunday_list_checked;
				
				int id = result.getInt(13);
				
				da = new Alarm_data(
						R.drawable.alarm,
						time,
						mon_image,
						tue_image,
						wed_image,
						thu_image,
						fri_image,
						sat_image,
						sun_image,
						am_pm,
						id);
				data.add(da);
				
				mon = 0;
				tue = 0;
				wed = 0;
				thu = 0;
				fri = 0;
				sat = 0;
				sun = 0;
				
				mon_image = R.drawable.monday_list;
				tue_image = R.drawable.tuesday_list; 
				wed_image = R.drawable.wednesday_list; 
				thu_image = R.drawable.thursday_list;
				fri_image = R.drawable.friday_list;
				sat_image = R.drawable.saturday_list; 
				sun_image = R.drawable.sunday_list;
				 
			}while(result.moveToNext());
		}	
		else
			data.add(null);
    	
	}  
    
	public static OnCreateContextMenuListener contextmenu = new OnCreateContextMenuListener(){

		@Override
		public void onCreateContextMenu(ContextMenu menu, View v,
				ContextMenuInfo menuinfo) {
			menu.add(0,1,0,"수정");
			menu.add(0,2,0,"삭제");
			
		}
		
	};
	
	@Override 
    public boolean onContextItemSelected(MenuItem item) { 
          
			
            
            switch(item.getItemId()) { 
            case 1: // 수정
            	Setting_alarm.ISNEW = true;
    			Intent edit = new Intent(MainActivity.this, Setting_alarm.class);
    			startActivity(edit);
                break; 
            case 2: // 삭제
            	//Setting_adapter.deleteAlarm(item);
                break; 
            default: 
                    return super.onContextItemSelected(item); 
            } 
            return true; 
    } 
	
	
}
